package com.zxx.order.presenter

import com.zqq.baselibrary.ext.excute
import com.zqq.baselibrary.http.rx.BaseSubscriber
import com.zqq.baselibrary.presenter.BasePresenter
import com.zxx.order.presenter.iview.ShipAddressView
import com.zxx.order.service.ShipAddressService
import com.zxx.order.service.bean.ShipAddress
import javax.inject.Inject

/**
 * 收货人列表Presenter
 * wc
 * @author zqq on 2019/6/30.
 */
class ShipAddressPresenter @Inject constructor() : BasePresenter<ShipAddressView>() {

    @Inject
    lateinit var shipAddressService: ShipAddressService

    /**
     * 获取收货人列表
     */
    fun getShipAddressList() {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        shipAddressService.getShipAddressList()
                .excute(object : BaseSubscriber<MutableList<ShipAddress>?>(mView) {
                    override fun onNext(t: MutableList<ShipAddress>?) {
                        super.onNext(t)
                        mView.onGetShipAddressResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        var list = getData()
                        mView.onGetShipAddressResult(list)
                    }
                }, lifecycleProvider)

    }

    /**
     * 设置默认收货人信息
     */
    fun setDefaultShipAddress(address: ShipAddress) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()

        shipAddressService.editShipAddress(address)
                .excute(object : BaseSubscriber<Boolean>(mView) {
                    override fun onNext(t: Boolean) {
                        super.onNext(t)
                        mView.onSetDefaultResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        mView.onSetDefaultResult(true)
                    }
                }, lifecycleProvider)

    }


    /**
     * 删除收货人信息
     */
    fun deleteShipAddress(id: Int) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()

        shipAddressService.deleteShipAddress(id)
                .excute(object : BaseSubscriber<Boolean>(mView) {
                    override fun onNext(t: Boolean) {
                        super.onNext(t)
                        mView.onDeleteResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        mView.onDeleteResult(true)
                    }
                }, lifecycleProvider)
    }


    private fun getData(): MutableList<ShipAddress> {

        var list = mutableListOf<ShipAddress>()
        list.add(ShipAddress(1, "狭小oaio", "135***25", "杭州西湖", 0))
        list.add(ShipAddress(2, "张理想", "139526255", "成都", 1))
        list.add(ShipAddress(3, "粒子哦", "139526255", "背景", 1))

        return list
    }

}